package cn.cdyx.common.interceptor;

import cn.cdyx.common.annotation.NotNeedInterceptor;
import cn.cdyx.common.exception.SystemException;
import cn.cdyx.common.pojo.Code;
import cn.cdyx.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * @Author Watermelon
 * @Date 2023-01-16 13:11
 * 系统拦截器
 */
@Component
@Slf4j
public class SystemInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        NotNeedInterceptor annotation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), NotNeedInterceptor.class);
        if(annotation!=null){
            log.info("使用了@NotNeedInterceptor不作处理！");
            return true;
        }else {
            return true;
            //throw new SystemException(Code.SELECT_ERR,"请求已拦截",null);
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
